﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Krist_em_up.Characters;
using Microsoft.Xna.Framework.Graphics;
using Krist_em_up.Characters.Enemies;
using Krist_em_up.Plateforms;
using Krist_em_up.Level;
using Krist_em_up.Item.Projectiles;

namespace Krist_em_up.Item
{
    public abstract class Projectile
    {
        protected TimeSpan lifeTime;

        protected Vector2 position, direction, origin;

        protected int shooter;

        protected Kristemup Game;

        protected Texture2D texture;

        protected SpriteBatch spriteBatch;

        protected float angle;

        /// <summary>
        /// Time remaining before the bullet disapear
        /// </summary>
        public TimeSpan LifeTime
        {
            get { return lifeTime; }
        }

        /// <summary>
        /// Badass who fired this bullet
        /// </summary>
        public int Shooter
        {
            get { return shooter; }
        }

        protected Vector2 speed;

        public Vector2 Speed
        {
            get { return speed; }
            set { speed = value; }
        }

        public Projectile(int shooter, Kristemup k, Vector2 position, Vector2 direction)
        {
            spriteBatch = new SpriteBatch(k.game.GraphicsDevice);

            this.position = position;
            this.direction = direction;
            this.Game = k;
            this.shooter = shooter;
        }

        /// <summary>
        /// Update the bullet
        /// </summary>
        /// <param name="gameTime">Time in game</param>
        public virtual void Update(GameTime gameTime)
        {
            lifeTime -= gameTime.ElapsedGameTime;

            angle = (float)Math.Atan(direction.Y / direction.X);

            if (lifeTime <= TimeSpan.Zero)
            {
                Game.bullets.Remove(this);
            }
            if (HandleCollision())
            {                
                Game.bullets.Remove(this);
            }
        }

        /// <summary>
        /// Draw the bullet on the screen
        /// </summary>
        /// <param name="gameTime"></param>
        public abstract void Draw(GameTime gameTime, Vector2 originG);

        /// <summary>
        /// Handle any collision with the plateforms or a opponent
        /// </summary>
        /// <returns>If the projectile has hit something in the map</returns>
        public virtual bool HandleCollision()
        {
            //Collision with plateforms
            if (Map.IsInBounds(Game.map.Plateforms, (int)(position.X / Plateform.Width), (int)(position.Y / Plateform.Height)) &&
                Game.map.Plateforms[(int)(position.X / Plateform.Width), (int)(position.Y / Plateform.Height)].Collision == TileCollision.Impassable)
                return true;
            else
            {
                if (shooter >= 0)
                {
                    foreach (Enemy e in Game.Enemies)
                    {
                        if (e.GetRec().Intersects(new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height)))
                        {
                            if (!(this is Grenade))
                                e.estTouche(Game.Enemies, shooter);
                            return true;
                        }
                    }

                }
                else
                {
                    for (int i = 0; i < Game.tabKris.Length; i++)
                    {
                        if (Game.tabKris[i].GetRec().Intersects(new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height)))
                        {
                            return Game.tabKris[i].Damaged(1);
                        }
                    }
                }

            }
            return false;

        }
        
    }
}
